package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/18 16:27
 */
@Service("workflowOperatorOwnerIdHandlerImpl")
public class WorkflowOperatorOwnerIdHandlerImpl implements WorkflowOperatorFieldHandler {

    @Resource
    private FormDataFormExplainHelper formDataFormExplainHelper;

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.OWNERID.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,
                                       WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        Integer businessType = workflowOperationsPojo.getBusinessType();
        Map<Long, List<String>> ownerIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (UserTeamEnum.hasTeam(businessType) && !data.containsKey(FieldTypeEnum.OWNERID.getAlias())) {
            List<Long> idIn = new ArrayList<>();
            idIn.add(paasFormDataEntity.getId());
            formDataFormExplainHelper.getUserIdMap(idIn, paasFormDataEntity.getCorpid(), businessType, false, ownerIdMap, coUserIdMap);

            List<String> ownerId = ownerIdMap.get(paasFormDataEntity.getId());
            if (Objects.isNull(ownerId) || ownerId.isEmpty()) {
                return null;
            }
            return ownerId;
        } else {
            return Collections.singletonList(paasFormDataEntity.getOwnerId());
        }
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,
                                         FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo,
                                         WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        // 不可以更新负责人
        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        // 不可以更新负责人
        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType, String corpid)  throws XbbException {
        Map<Long, List<String>> coUserIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> ownerIdMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (UserTeamEnum.hasTeam(businessType)) {
            List<Long> idIn = new ArrayList<>();
            idIn.add(formDataEntity.getId());
            formDataFormExplainHelper.getUserIdMap(idIn, formDataEntity.getCorpid(), businessType, false, ownerIdMap, coUserIdMap);

            List<String> ownerId = ownerIdMap.get(formDataEntity.getId());
            if (Objects.isNull(ownerId) || ownerId.isEmpty()) {
                return null;
            }
            return ownerId;
        } else {
            return Collections.singletonList(formDataEntity.getOwnerId());
        }
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        // 不可以更新负责人
        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
    }
}
